<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .header {
            width: 500px;
            margin: 0 auto;
            background-color: #f4f4f4;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-shadow: 1px 1px 5px;
            min-height: 500px;
        }

        .box {
            width: 480px;
            height: 80px;
            outline: none;
            border: none;
            margin-bottom: 10px;
            border-bottom: 1px solid #999;
        }

        ul li {
            list-style: none;
            border-bottom: 1px solid #999;
            height: 60px;
            width: 480px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        span {
            flex-grow: 1;
        }

        button {
            background-color: red;
            height: 30px;
            width: 30px;
        }
    </style>
</head>

<body>
    <div class="header">
        <input type="text" class="box" placeholder="请输入清单">
        <ul class="list">
        </ul>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        //1. 渲染列表
        var arr = [];
        function rendList() {
            var str = '';
            arr.forEach(function (item, index) {
                str +=
                    `
            <li>
                <span>${index+1}.${item}</span>
                <button data-index='${index}'>X</button>
            </li>
            `;
            })
            $('.list').html(str);
        }
        rendList();

        //给输入框绑定keypress
        $('.box').on('keypress', function () {
            //判断按下enter键
            if (event.key === 'Enter') {
                // 获取输入框的内容
                var text =$(this).val();
                //把输入框内容放入arr数组,然后重新渲染
                arr.push(text);
                //重新渲染列表
                rendList();
                //清空输入框
                $(this).val('');
            }
        })
        //绑定事件,需要使用事件托管,不然绑定不了,因为绑定的时候,还没有button
        $('.list').on('click','button',function(){
            var index = $(this).data('index');
            arr.splice(index,1);
            rendList();
        })
    </script>

    <!-- <script>
        
        var Input = document.querySelector('input');
        var Ul = document.querySelector('ul');

        function pressKey() {
            var name = event.code;
            var len = document.querySelectorAll('li').length;
            if (name == 'Enter') {

                Ul.innerHTML +=
                    `
                        <li>
                            <span>${len+1}.${Input.value}</span>
                            <button onclick="deletes()">X</button>
                        </li>
                    `
                Input.value = ''
            }
   
        }
        function deletes() {
                event.target.parentNode.remove()
            }
    </script> -->
</body>

</html>